﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace Coding4Fun.WheresMyCar
{
	public partial class SettingsPage : PhoneApplicationPage, INotifyPropertyChanged
	{
		private WheresTheCarSettings _settings;
		public WheresTheCarSettings Settings
		{
			get { return _settings; }
			set
			{
				_settings = value;
				RaisePropertyChanged("Settings");
			}
		}

        private ObservableCollection<string> _mapModes;
        public ObservableCollection<string> MapModes
        {
            get { return _mapModes; }
            set
            {
                _mapModes = value;
                RaisePropertyChanged("MapModes");
            }
        }

		private ObservableCollection<string> _measurementUnits;
		public ObservableCollection<string> MeasurementUnits
		{
			get { return _measurementUnits; }
			set
			{
				_measurementUnits = value;
				RaisePropertyChanged("MeasurementUnits");
			}
		}

		private string _measurementUnit;
		public string MeasurementUnit
		{
			get { return _measurementUnit; }
			set { _measurementUnit = value;
				RaisePropertyChanged("MeasurementUnit");
			}
		}

        private string _mapMode;
        public string MapMode
        {
            get { return _mapMode; }
            set
            {
                _mapMode = value;
                RaisePropertyChanged("MapMode");
            }
        }

		public SettingsPage()
		{
			InitializeComponent();
			Settings = WheresTheCarSettings.Instance;
			this.DataContext = this;

			this.Loaded += new RoutedEventHandler(SettingsPage_Loaded);
		}

		void SettingsPage_Loaded(object sender, RoutedEventArgs e)
		{
			this.NavigationService.Navigating += new System.Windows.Navigation.NavigatingCancelEventHandler(NavigationService_Navigating);
			MeasurementUnits = new ObservableCollection<string> { "Imperial (miles)", "Metric (km)" };
			MeasurementUnit = MeasurementUnits[(int)Settings.MeasurementMode];

            MapModes = new ObservableCollection<string> { "Aerial View", "Street Map View" };
            MapMode = Settings.UseAerialMode ? "Aerial View" : "Street Map View";

		}

		void NavigationService_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
		{
			if (e.NavigationMode == NavigationMode.Back)
			{
				if( MeasurementUnit.StartsWith("Imp") ) this.Settings.MeasurementMode = MeasurementMode.Imperial;
				else this.Settings.MeasurementMode = MeasurementMode.Metric;

                this.Settings.UseAerialMode = MapMode.StartsWith("Aer");

				this.Settings.Save();
			}
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected void RaisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}